<?php

namespace app\admin\controller;
use app\admin\model\Api;

class GoodsAttrController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }
    /**
     * author：廖桂钦
     * eamil : artistCode@163.com
     * create time :2020/4/8
     * description[读取api 列表]
     */

    public function read(){
       $Api =  new Api();
       $data = $Api->select();

        if (isset($_GET['type'])&& !empty($_GET['type'])){
            $alias = ['api_name' => 'name', 'api_parent' => 'pid', 'api_id' => 'id'];
            $tree_list = tree($data, 'api_id', $alias);
            array_unshift($tree_list, ['name' => '顶级栏目', 'pid' => 0, 'id' => 0]);
            return json($tree_list);
        }

        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' => 100));
    }

    /**
     * author：廖桂钦
     * eamil : artistCode@163.com
     * create time :2020/4/8
     */
    public function add(){

        $Api =  new Api();
        $post_data= input('post.');

        $data= $Api->save($post_data);
            if ($data) {
                return json(['code' => 0, 'msg' => '添加成功']);
            } else {
                return json(['code' => 500, 'msg' => '添加失败']);
            }
    }
    public function upd()
    {
        $Api= (new Api());
        $post_data = input('post.');
        $Api->isUpdate(true)->save($post_data);
        if ($Api) {
            return json(['code' => 0, 'msg' => '更新成功']);
        } else {
            return json(['code' => 500, 'msg' => '更新失败']);
        }
    }
    public function  del(){

        $result =Api::get(input('post.'))->delete();
        if($result){
            return json(['code'=>0,'msg'=>'删除成功']);
        }else{
            return json(['code'=>500,'msg'=>'删除失败']);
        }
    }

}
